<template>
  <div>
    <el-card style="width: 600px;height: 150px;margin: 0 auto;
    background-color: rgba(255,255,255,0.3)">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="车牌" prop="carId">
          <el-input v-model="ruleForm.carId"></el-input>
        </el-form-item>
        <el-form-item style="position: relative;right: 45px;">
          <el-button type="primary" @click="carIn()">提交</el-button>
          <el-button type="primary" @click="open()">申述</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-divider></el-divider>

    <el-table
        :data="editForm"
        style="width: 100%" v-if=this.change>
      <el-table-column prop="id" label="编号" header-align="center" align="center" width="180"></el-table-column>
      <el-table-column prop="ownerName" label="姓名" header-align="center" align="center" width="180"></el-table-column>
      <el-table-column prop="carId" header-align="center" align="center" label="车牌号"></el-table-column>
      <el-table-column prop="ownerPhone" header-align="center" align="center" label="电话"></el-table-column>
    </el-table>


    <el-dialog title="申诉信息" :visible.sync="dialogFormVisible">
      <el-form :model="allega" :rules="rules1" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="您的姓名" prop="realName">
          <el-input v-model="allega.realName"></el-input>
        </el-form-item>
        <el-form-item label="车位编号" prop="locationNumber">
          <el-input v-model="allega.locationNumber"></el-input>
        </el-form-item>
        <el-form-item label="对方车牌号" prop="carId">
          <el-input v-model="allega.carId"></el-input>
        </el-form-item>
        <el-form-item label="申诉内容" prop="connect">
          <el-input type="textarea" v-model="allega.connect"></el-input>
        </el-form-item>
        <el-form-item>
          <el-upload
              style="position: relative;right: 45px"
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item style="position: relative;right: 45px;">
          <el-button type="primary" @click="addnew()">提交</el-button>
          <el-button @click="cancel()">取消</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      ruleForm: {
        carId: '',
      },
      change: false,
      editForm: [{
        id: '',
        carId: '',
        ownerPhone: '',
        ownerName: ''
      }],
      allega: {
        realName: '',
        locationNumber: '',
        carId: '',
        connect: '',
        cardId: ''
      },
      rules1: {
        realName: [
          {required: true, message: '输入您的姓名', trigger: 'blur'},
          {min: 0, max: 10, message: '长度在2到10以内', trigger: 'blur'}
        ],
        locationNumber: [
          {required: true, message: '输入车位号', trigger: 'blur'}
        ],
        carId: [
          {required: true, message: '输入对方车牌号', trigger: 'blur'}
        ],
        connect: [
          {required: true, message: '输入申述内容', trigger: 'blur '}
        ],

      },
      rules: {
        carId: [
          {required: true, message: '输入车牌', trigger: 'blur'},
          {min: 7, max: 7, message: '输入正确车牌', trigger: 'blur'}
        ],

      }
    };
  },
  methods: {
    cancel() {
      this.dialogFormVisible = false
    },
    addnew() {
      let url = 'http://localhost:8080/allega/newAllega'
      this.allega.cardId = localStorage.getItem('cardId')
      console.log(this.allega)
      this.axios.post(url, this.allega).then((response) => {
        let responseBody = response.data
        if (responseBody.code == 200) {
          this.$message({
            showClose: true,
            message: '申诉信息成功等待处理！',
            type: 'success'
          });
          this.dialogFormVisible = false
        } else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'error'
          });
        }
      });
    },
    open() {
      this.dialogFormVisible = true
    },
    carIn() {

      let url = 'http://localhost:8080/user/spaceOccupancy?carId=' + this.ruleForm.carId
      this.axios.post(url, this.ruleForm).then((response) => {
        let responseBody = response.data;
        if (responseBody.code == 200) {
          this.change = true
          this.editForm = responseBody.data;
          console.log(this.editForm)
          this.$message({
            showClose: true,
            message: '查询成功！',
            type: 'success'
          });
        } else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'error'
          });
        }
      })
    }
  }
}
</script>
